package day3.jdbc.dao.refactor;

import java.sql.ResultSet;
import java.sql.SQLException;

import day1.domain.User;

public class UserDaoImpl2 {

	private MyDaoTemplate template=new MyDaoTemplate();
	
	public static void main(String[] args) throws SQLException,
		InterruptedException {
		int id=4;
		UserDaoImpl2 userDao=new UserDaoImpl2();
		for(int i=0;i<10;i++){
			System.out.println(i+" = "+userDao.findById(id));
		}
		
		System.out.println(userDao.findNameById(id));
	}
	
	public User findById(int id){
		String sql="select name,id,money from user where id=?";
		User user=(User) template.find(sql, new Object[]{id}, new UserRowMapper());
		return user;
	}
	
	public String findNameById(int id){
		String sql="select name from user where id=?";
		String name=(String) template.find(sql, new Object[]{id}, new RowMapper(){
			public Object mapRow(ResultSet rs) {
				// TODO Auto-generated method stub
				try {
					//System.out.println("in noname class");
					if(rs.next()){
						return rs.getString("name");
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
			}
		
		});
		return name;
	}
}
